﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Data.Objects;

namespace OptusMobile.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
           
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 20;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            Session.Abandon();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            DisposeObjectContext();
        }

        /// <summary>
        /// Disposes the shared ObjectContext instance.
        /// </summary>
        public static void DisposeObjectContext()
        {
            if (HttpContext.Current == null)
                throw new InvalidOperationException("DisposeObjectContext() can only be used in a HTTP context.");

            string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
            if (HttpContext.Current.Items.Contains(ocKey))
            {
                ObjectContext objectContext = HttpContext.Current.Items[ocKey] as ObjectContext;
                if (objectContext != null)
                    objectContext.Dispose();
                HttpContext.Current.Items.Remove(ocKey);

                System.Diagnostics.Debug.WriteLine("AspNetObjectContextManager: Disposed NorthwindObjectContext");
            }
        }
    }
}